import pandas as pd

from quantification.data_handler.DataHandler import DataHandler
from quantification.data_handler.data.FileUtil import FileUtil
from quantification.data_handler.Order import Order


class DealRecord(DataHandler):
    __slots__ = ('__stock_code', '__deal_serial_number', '__order_serial_number', '__deal_direction',
                 '__plans', '__price', '__cost', '__fee', '__commission', '__deal_amount', '__deal_time')
    '''
    交易记录表
    '''
    DEAL_RECORD_FILE_NAME = 'deal_record.csv'

    def __init__(self, stock_code,order: Order):
        self.__stock_code = stock_code

        # 交易流水号
        self.__deal_serial_number = None

        # 持仓流水号
        self.__position_serial_number = None

        # 订单流水
        self.__order_serial_number = None

        # 交易方向1：买入，-1：卖出  0:不操作
        self.__deal_direction = None

        # 成交数量（股）
        self.__plans = 0

        # 股价
        self.__price = None

        # 成本价（股价+手续费+佣金）
        self.__cost = None

        # 手续费
        self.__fee = None

        # 佣金
        self.__commission = None

        # 买入成交金额（成本价*成交数量）
        self.__deal_amount = None

        # 成交时间
        self.__deal_time = None

    def load_data(self, start_date=None, end_date=None):
        return pd.read_csv(FileUtil.get_file_url(DealRecord.DEAL_RECORD_FILE_NAME))

    def store_data(self, pd_data, overwrite=True):
        # with atomic_overwrite("df.csv") as f:
        #     pd_data.to_csv(f)
        if overwrite:
            pd_data.to_csv(DealRecord.DEAL_RECORD_FILE_NAME)
        else:
            pd_data.to_csv(DealRecord.DEAL_RECORD_FILE_NAME, mode='a')


